//
//  ViewController.m
//  UI05_UIControl子类
//
//  Created by LGC on 16/3/15.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "ViewController.h"
#import "ImageViewOfAnimation.h"
#import "MySwitch.h"
#import "MySlider.h"
#import "MySegment.h"
#import "MyPageControl.h"
#import "MyButton.h"

@interface ViewController ()
@property (nonatomic, retain) ImageViewOfAnimation *imageView;

@property (nonatomic, retain) MySwitch *switchOfAnimation;

@property (nonatomic, retain) MySlider *slider;

@end

@implementation ViewController

#pragma mark - 重写dealloc
-(void)dealloc {
    [_imageView release];
    [_switchOfAnimation release];
    [_slider release];
    [super dealloc];
}

#pragma mark - 已经加载
- (void)viewDidLoad {
    [super viewDidLoad];
    [self createImageView];
    [self createSwitch];
    [self createSlider];
    [self createSegment];
    [self createPageControl];
    [self createButton];
    
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)createImageView {
    UIImage *image = [UIImage imageNamed:@"angry_00.jpg"];
    
    self.imageView = [[ImageViewOfAnimation alloc]initWithImage:image];
    
    self.imageView.frame = CGRectMake( 0, 0, 350, 500);
    
    self.imageView.center = self.view.center;
    
    [self.view addSubview:self.imageView];
    
    [_imageView release];

}


#pragma mark - 知识点1: UISwitch

- (void)createSwitch {
    self.switchOfAnimation = [[MySwitch alloc] initWithFrame:CGRectMake(40, 640, 0, 0)];
    
    [self.view addSubview:self.switchOfAnimation];
    
    [_switchOfAnimation release];
    
    // 添加事件, 注意:控制事件是 值变化.
    [self.switchOfAnimation addTarget:self action:@selector(handleUp:) forControlEvents:UIControlEventValueChanged];

}

/** UISwith的事件 */
- (void)handleUp:(MySwitch *)mySwitch{
    
    NSMutableArray *array = [NSMutableArray array];
    
    if (mySwitch.on) {
        // 开始动画.
        for (int i = 0; i < 26; i++) {
            
            NSString *name = [NSString stringWithFormat:@"angry_%02d", i];
            // 创建文件路径
            NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"jpg"];
            // 内存比imageNamed:方法优化.
            UIImage *image = [UIImage imageWithContentsOfFile:path];
            
            [array addObject:image];
 
        }
        
        self.imageView.animationImages = array;
        
        self.imageView.animationDuration = (self.slider.maximumValue - self.slider.value);
        
        self.imageView.animationRepeatCount = 0;
        
        [self.imageView startAnimating];
        
        NSLog(@"开启");
    }
    
    else {
        
        [self.imageView stopAnimating];
        
        /** imageView的动态图像指向空 */
        self.imageView.animationImages = nil;
        
        [array removeAllObjects];
        
        NSLog(@"关闭");
    }
    


}

#pragma mark - 知识点2: UISlider

- (void)createSlider {
    
    self.slider = [[MySlider alloc]initWithFrame:CGRectMake(40, 680, 300, 40)];
    
    [self.view addSubview:self.slider];
    
    [_slider release];
  
    // 最小, 最大值赋值/
    self.slider.minimumValue = 1;
    
    self.slider.maximumValue = 10;
    
    /** 设置slider从特定的value值开始 */
    [self.slider setValue:(self.slider.maximumValue  + 1)/ 2.f animated:YES];
    
    /** slider添加事件 */
    [self.slider addTarget:self action:@selector(handleSlider:) forControlEvents:UIControlEventValueChanged];
    
}

/** slider 事件 */
- (void)handleSlider :(MySlider *)slider {
    
    // 当动画执行时.
    if (self.switchOfAnimation.on) {
        
        self.imageView.animationDuration = (slider.maximumValue + 1 - slider.value);
        
        [self.imageView startAnimating];
    }
}

#pragma mark - 知识点3: UISegmentedControl

- (void)createSegment {
    
    // 数组中放入字符串对象或Image对象.
    MySegment *segment = [[MySegment alloc]initWithItems:@[@"红色", @"黄色", @"蓝色"]];
    
    segment.frame = CGRectMake(50, 50, 300, 40);
    
    [self.view addSubview:segment];
    
    [segment release];
    
    /** segment 添加事件 */
    [segment addTarget:self action:@selector(handleSegment:) forControlEvents:UIControlEventValueChanged];
    
}

/** segment 事件 */
- (void)handleSegment :(MySegment *)segment {
    
    switch (segment.selectedSegmentIndex) {
        case 0:
            self.view.backgroundColor = [UIColor redColor];
            break;
        case 1:
            self.view.backgroundColor = [UIColor yellowColor];
            break;
        case 2:
            self.view.backgroundColor = [UIColor blueColor];
            break;
        default:
            break;
    }
}

#pragma mark - 知识点4: UIPageControl

- (void)createPageControl {
    MyPageControl *pageControl = [[MyPageControl alloc]initWithFrame:CGRectMake(self.imageView.frame.size.width / 3.2, self.imageView.frame.size.height - 40, self.imageView.self.frame.size.width / 3.6, 40)];
    
    [self.imageView addSubview:pageControl];
    
    [pageControl release];
    
    // 设置页数.
    pageControl.numberOfPages = 5;
    
    [pageControl addTarget:self action:@selector(handlePageControl:) forControlEvents:UIControlEventValueChanged];
    
    
}

- (void)handlePageControl: (MyPageControl *)pageControl {
    
}

#pragma mark - Button

- (void)createButton {
    
    MyButton *button = [MyButton buttonWithType:UIButtonTypeSystem];
    
    button.frame = CGRectMake(120, 630, 60, 40);
    
     /** 正常状态下selected属性为0, title显示为"开启" */
    [button setTitle:@"开启" forState:UIControlStateNormal];
     /** 在selected属性为1后, 在UIControlStateSelected状态下 title显示为"关闭" */
    [button setTitle:@"关闭" forState:UIControlStateSelected];
    
    [self.view addSubview:button];
    
    [button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
    

}

/** **************************************************** */
- (void)handleButton:(MyButton *)button {
    
    // UIControl有个Selected属性(BOOL类型).
    // selected 属性 默认为0.
    // 每点击一次selected属性取反.改变button状态.
    button.selected = !button.selected;
    // 点击之后         // 点击之前
    /** 如果button的selected为1 */
    if (button.isSelected) {
        
        button.backgroundColor = [UIColor yellowColor];
    }
    else {
        
        button.backgroundColor = [UIColor blueColor];
    }
}
/** **************************************************** */




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
